package com.zhongwang.mms.module.tool.service;

import com.zhongwang.mms.module.tool.dao.AccToolStatisDao;
import com.zhongwang.mms.module.tool.model.AccToolStatisModel;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AccToolStatisService {
    @Autowired
    private AccToolStatisDao accToolStatisDao;

    /**
     * 查询数据的集合
     * @param model
     * @return
     */
    public List<AccToolStatisModel> selectAccToolStatisList(AccToolStatisModel model){
        return accToolStatisDao.selectAccToolStatisList(model);
    }


    /**
     * 添加
     * @param model
     * @return
     */
    public int insert(AccToolStatisModel model){
        return  accToolStatisDao.insert(model);
    }

    /** 修改数据
     *
     * @param model
     * @return
     */
    public int update(AccToolStatisModel model){
        return accToolStatisDao.update(model);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int del(Integer id){
        return  accToolStatisDao.del(id);
    }

    @Value("${user.upload.path}")
    private String uploadPath;
    /**
     * 保存文件
     *
     * @param file project
     * @param file file
     */
    public int saveFile(MultipartFile[] file,String statisDate,String statisDetail,String statisType) {
        int flg=0;
        if (null != file && file.length > 0) {
            //准备新增数据的实体
            AccToolStatisModel model=new AccToolStatisModel();
            model.setStatisUpUser(ShiroUtils.getLoginName());
            model.setStatisDate(statisDate);
            model.setStatisDetail(statisDetail);
            //大的归类
            model.setStatisType(statisType);

            String id="";
            //获取当前时间戳
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
            String temp = sf.format(new Date());
            //获取6位随机数
            int random=(int) ((Math.random()+1)*100000);
            String folder=String.valueOf(temp+random);

            String path = uploadPath + "/"+folder+"/";
            for (MultipartFile f : file) {

                String fileName = FileUtils.upload(f, path);
                if (fileName!=null && fileName!="") {
                    String url = "files/"+folder+"/" + fileName;
                    //**********************这块是数据库写数据的地方哈*******
                    //这个url就是存储到数据的路径
                    String dbFiled=url;  //这句话变成实体的字段值=url;
                    //文件名
                    model.setStatisFileType(fileName);
                    model.setStatisPath(url);
                     flg=accToolStatisDao.insert(model);
                    //**********************这块是数据库写数据的地方哈*******
                }
            }
        }
        return flg;
    }

    /**
     * 保存文件
     *
     * @param file project
     * @param file file
     */
    public int editFile(MultipartFile[] file,String statisDate,String statisDetail,Integer statisId) {
        //准备修改数据的实体
        AccToolStatisModel model=new AccToolStatisModel();
        model.setStatisUpUser(ShiroUtils.getLoginName());
        model.setStatisDate(statisDate);
        model.setStatisDetail(statisDetail);
        //修改时，主键的id
        model.setStatisId(statisId);
        try{
            if (null != file && file.length > 0) {

                String id = "";
                //获取当前时间戳
                SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                String temp = sf.format(new Date());
                //获取6位随机数
                int random = (int) ((Math.random() + 1) * 100000);
                String folder = String.valueOf(temp + random);

                String path = uploadPath + "/" + folder + "/";
                for (MultipartFile f : file) {

                    String fileName = FileUtils.upload(f, path);
                    if (fileName != null && fileName != "") {
                        String url = "files/" + folder + "/" + fileName;
                        //**********************这块是数据库写数据的地方哈*******
                        //这个url就是存储到数据的路径
                        String dbFiled = url;  //这句话变成实体的字段值=url;
                        //文件名
                        model.setStatisFileType(fileName);
                        model.setStatisPath(url);

                        //**********************这块是数据库写数据的地方哈*******
                    }
                }
            }
        }catch(Exception e){
            String a=e.getMessage();
        }
        int flg=accToolStatisDao.update(model);
        return flg;
    }
}
